package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.rank.UnionActivityRankListModel;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CROSS_GENERAL_ACTIVITY_GET_UNION_RANK_REWARD, desc = "跨服活动商会排行领奖")
public class CrossGeneralActivityGetUnionRankRewardCmd extends AbstractCommand {

    private int activityId;
    private int conditionId;

    private ActivityRewardResult rewardResult;
    private boolean isNeedResp = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityProto.ActivityGetRewardReqMsg req = ActivityProto.ActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        conditionId = req.getConditionId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            rewardResult = new ActivityRewardResult();
            rewardResult.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return;
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME;
            return;
        }
        //本服排行奖励
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
       /* if(activityConditionInfo != null && ActivityHelper.isUnionRankType(activityConditionInfo.getType()) && !ActivityHelper.conditionTypeIsCrossRank(activityConditionInfo.getType())){
            int conditionType = activityConditionInfo.getType();
            UnionActivityConditionData conditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, conditionType, player.getUserId());
            String unionId = "";
            if (conditionData != null) {
                unionId = conditionData.getUnionUid();
            }
            UnionActivityRankListModel unionActivityRankListModel = GeneralActivityRankMgr.getUnionRankListModel(activityId, conditionType);
            if(unionActivityRankListModel != null) {
                int myRank = unionActivityRankListModel.getMyRank(unionId);
                rewardResult = player.getModule(ActivityModule.class).getCrossGeneralUnionRankActivityReward(activityId, conditionId, true, myRank);
            }
            return;
        }*/
        rewardResult = player.getModule(ActivityModule.class).getCrossGeneralUnionRankActivityReward(activityId, conditionId, false, 0);
        if (rewardResult.getRet() == 0) {
            isNeedResp = false;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (isNeedResp) {
            ActivityProto.ActivityGetRewardRespMsg.Builder respMsg = ActivityPb.parseActivityGetRewardRespMsg(rewardResult);
            if (null != respMsg) {
                player.sendPacket(Protocol.U_CROSS_GENERAL_ACTIVITY_GET_UNION_RANK_REWARD, respMsg);
            }
        }
    }
}